package hn.cch.advanced.concurrent;

import java.util.HashMap;
import java.util.UUID;

/**
 * 线程安全:并发问题
 */
public class ConcurrentHashMapDemo implements Runnable {

    @Override
    public void run() {

        for (int i = 0; i < 1000; i++) {
            // 获得当前线程的名称
            String key = Thread.currentThread().getName() + "=key_" + i;
            map.put(key, UUID.randomUUID().toString());
            System.out.println(key + " size:" + map.size());
        }


    }


    // public static ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>();
    public static HashMap<String, String> map = new HashMap<>();

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(new ConcurrentHashMapDemo(), "thread-" + i).start();
        }


    }

}